package com.cyt.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cyt.dao.impl.SPFeiLeiDaoImpl;
import com.cyt.dao.impl.ShangPingXXTwoDaoImpl;
import com.cyt.dao.impl.XingWengXiZhiTwoDaoImpl;
import com.cyt.entity.ShangPingXX;
import com.cyt.entity.XingWengXiZhi;
import com.cyt.util.BaseServlet;
import com.cyt.util.Result;
import com.google.gson.Gson;


/**
 * 分页 
 * 查询所有商品的信息 多条件查询
 * @author 喜悦
 *localhost:8080/shiping/adminChanPinXiangQin?reqName=getChanPinXiangQin
 */
public class AdminChanPinXiangQinServlet extends BaseServlet {
	public void getChanPinXiangQin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int cPid=0;
		if(request.getParameter("cPid")!=null && !request.getParameter("cPid").equals("")) {
			cPid=Integer.parseInt(request.getParameter("cPid"));
		}
		String cPName=request.getParameter("cPName");
		String cPCaiZhi=request.getParameter("cPCaiZhi");
		int qiShiCPShoujia=0;
		if(request.getParameter("qiShiCPShoujia")!=null && !request.getParameter("qiShiCPShoujia").equals("")) {
			qiShiCPShoujia=Integer.parseInt(request.getParameter("qiShiCPShoujia"));
		}
		int jieShuCPShoujia=0;
		if(request.getParameter("jieShuCPShoujia")!=null && !request.getParameter("jieShuCPShoujia").equals("")) {
			jieShuCPShoujia=Integer.parseInt(request.getParameter("jieShuCPShoujia"));
		}
		String cPShiChangSJ=request.getParameter("cPShiChangSJ");
		String cPPingPai=request.getParameter("cPPingPai");
		String xingHao=request.getParameter("xingHao");
		String yanSe=request.getParameter("yanSe");
		String dingZhi=request.getParameter("dingZhi");
		String chuanYiRenQun=request.getParameter("chuanYiRenQun");
		int fengLeiId=0;
		if(request.getParameter("fengLeiId")!=null && !request.getParameter("fengLeiId").equals("")) {
			fengLeiId=Integer.parseInt(request.getParameter("fengLeiId"));
		}
		String cPImg=request.getParameter("cPImg");
		

		
		ShangPingXX sp=new ShangPingXX(cPid, cPName, cPCaiZhi, 0, qiShiCPShoujia, jieShuCPShoujia, 0, cPPingPai, xingHao, yanSe, dingZhi, chuanYiRenQun, fengLeiId, cPImg, "");
		 int page=Integer.parseInt(request.getParameter("page"));
		 int size=Integer.parseInt(request.getParameter("size"));
		ShangPingXXTwoDaoImpl spXiangQingDaoi=new ShangPingXXTwoDaoImpl();
	     List <ShangPingXX> spXQLists= spXiangQingDaoi.getShangPinXiangQinPage(sp, page, size);
	
	
	     response.setCharacterEncoding("UTF-8");
	     PrintWriter out=response.getWriter();
	
	     Gson gson=new Gson();
	     String res =gson.toJson(spXQLists);
	     
	     out.print(res);
	     
	     out.flush();
	     out.close();
	}

	/**
	 * localhost:8080/shiping/adminChanPinXiangQin?reqName=getChanPinXiangQinTiao
	 * 查询所有表中数据条数
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getChanPinXiangQinTiao(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int cPid=0;
		if(request.getParameter("cPid")!=null && !request.getParameter("cPid").equals("")) {
			cPid=Integer.parseInt(request.getParameter("cPid"));
		}
		String cPName=request.getParameter("cPName");
		String cPCaiZhi=request.getParameter("cPCaiZhi");
		int qiShiCPShoujia=0;
		if(request.getParameter("qiShiCPShoujia")!=null && !request.getParameter("qiShiCPShoujia").equals("")) {
			qiShiCPShoujia=Integer.parseInt(request.getParameter("qiShiCPShoujia"));
		}
		int jieShuCPShoujia=0;
		if(request.getParameter("jieShuCPShoujia")!=null && !request.getParameter("jieShuCPShoujia").equals("")) {
			jieShuCPShoujia=Integer.parseInt(request.getParameter("jieShuCPShoujia"));
		}
		String cPShiChangSJ=request.getParameter("cPShiChangSJ");
		String cPPingPai=request.getParameter("cPPingPai");
		String xingHao=request.getParameter("xingHao");
		String yanSe=request.getParameter("yanSe");
		String dingZhi=request.getParameter("dingZhi");
		String chuanYiRenQun=request.getParameter("chuanYiRenQun");
		int fengLeiId=0;
		if(request.getParameter("fengLeiId")!=null && !request.getParameter("fengLeiId").equals("")) {
			fengLeiId=Integer.parseInt(request.getParameter("fengLeiId"));
		}
		String cPImg=request.getParameter("cPImg");
		

		
		ShangPingXX sp=new ShangPingXX(cPid, cPName, cPCaiZhi, 0, qiShiCPShoujia, jieShuCPShoujia, 0, cPPingPai, xingHao, yanSe, dingZhi, chuanYiRenQun, fengLeiId, cPImg, "");
	
		ShangPingXXTwoDaoImpl spXiangQingDaoi=new ShangPingXXTwoDaoImpl();
	     List <ShangPingXX> spXQLists= spXiangQingDaoi.getShangPinXiangQin(sp);
	
	
	
	     response.setCharacterEncoding("UTF-8");
	     PrintWriter out=response.getWriter();
	
	     Gson gson=new Gson();
	     String res =gson.toJson(spXQLists.size());	     
	     out.print(res);
	     
	     out.flush();
	     out.close();
	}
	
	/**
	 * 删除产品
	 * localhost:8080/shiping/adminChanPinXiangQin?reqName=shanChuChanPinXiangQin
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void shanChuChanPinXiangQin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    int shangId=Integer.parseInt(request.getParameter("categoryId"));
	    ShangPingXXTwoDaoImpl spXiangDaoi=new ShangPingXXTwoDaoImpl();
	    int n =spXiangDaoi.shanChuChanPinZhu(shangId);
	    response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
	    if (n>0) {
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", ""));

		}
	     
	    out.flush();
	    out.close();
	}
	
	/**
	 * localhost:8080/shiping/adminChanPinXiangQin?reqName=shanChuChanPinXiangQinS
	 * 批量删除
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void shanChuChanPinXiangQinS(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 response.setCharacterEncoding("UTF-8");
		 PrintWriter out=response.getWriter();
		 ShangPingXXTwoDaoImpl spXiangDaoi=new ShangPingXXTwoDaoImpl();

		 String ids=request.getParameter("ids");
	     int n =spXiangDaoi.shanChanPinXQS(ids);
	   
	    if (n>0) {
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", ""));

		}
	     
	    out.flush();
	    out.close();
	}
	
	/**
	 * 添加产品
	 * localhost:8080/shiping/adminChanPinXiangQin?reqName=AddChanPinXiangQin
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void AddChanPinXiangQin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
	    ShangPingXXTwoDaoImpl chanPinDaoi=new ShangPingXXTwoDaoImpl();
		String cPName=request.getParameter("cPName");
		String cPCaiZhi=request.getParameter("cPCaiZhi");
		int cPShoujia=0;
		if(request.getParameter("cPShoujia")!=null && !request.getParameter("cPShoujia").equals("")) {
			cPShoujia=Integer.parseInt(request.getParameter("cPShoujia"));
		}
		int cPShiChangSJ=0;
		if(request.getParameter("cPShiChangSJ")!=null && !request.getParameter("cPShiChangSJ").equals("")) {
			cPShiChangSJ=Integer.parseInt(request.getParameter("cPShiChangSJ"));
		}
		String cPPingPai=request.getParameter("cPPingPai");
		String xingHao=request.getParameter("xingHao");
		String yanSe=request.getParameter("yanSe");
		String dingZhi=request.getParameter("dingZhi");
		String chuanYiRenQun=request.getParameter("chuanYiRenQun");
		int fengLeiId=0;
		if(request.getParameter("fengLeiId")!=null && !request.getParameter("fengLeiId").equals("")) {
			fengLeiId=Integer.parseInt(request.getParameter("fengLeiId"));
		}
		String cPImg=request.getParameter("cPImg");
		ShangPingXX spxq= new ShangPingXX(0, cPName, cPCaiZhi, cPShoujia, cPShiChangSJ, cPPingPai, xingHao, yanSe, dingZhi, chuanYiRenQun, fengLeiId, cPImg);
        System.out.println(spxq);

		int n= chanPinDaoi.addChanPinXQ(spxq);
		
	    if (n>0) {
			out.print(Result.toClient(true, "添加成功", ""));
		} else {
			out.print(Result.toClient(false, "添加失败", ""));

		}
		
		
	     
	    out.flush();
	    out.close();
	}
	
	/**
	 * 通过id查数据,前台用于数据回显
	 * localhost:8080/shiping/adminChanPinXiangQin?reqName=chaChanPinXiangQinId
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void chaChanPinXiangQinId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
	    
	    ShangPingXXTwoDaoImpl xinWenDaoi=new ShangPingXXTwoDaoImpl();
		int spId=Integer.parseInt(request.getParameter("cPid"));
		ShangPingXX cpxq= xinWenDaoi.chaShangPingXXId(spId);
        
		Gson gson=new Gson();
		String res =gson.toJson(cpxq);
	    out.print(res);
	    
	    
	    out.flush();
	    out.close();
	}
	
	
	/**
	 * 修改产品
	 * localhost:8080/shiping/adminChanPinXiangQin?reqName=xiuGaiChanPinXiangQin
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void xiuGaiChanPinXiangQin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
	    
	    ShangPingXXTwoDaoImpl spxxDaoi=new ShangPingXXTwoDaoImpl();
		int cPid=0;
		if(request.getParameter("cPid")!=null && !request.getParameter("cPid").equals("")) {
			cPid=Integer.parseInt(request.getParameter("cPid"));
		}
		String cPName=request.getParameter("cPName");
		String cPCaiZhi=request.getParameter("cPCaiZhi");
		int cPShoujia=0;
		if(request.getParameter("cPShoujia")!=null && !request.getParameter("cPShoujia").equals("")) {
			cPShoujia=Integer.parseInt(request.getParameter("cPShoujia"));
		}
		int cPShiChangSJ=0;
		if(request.getParameter("cPShiChangSJ")!=null && !request.getParameter("cPShiChangSJ").equals("")) {
			cPShiChangSJ=Integer.parseInt(request.getParameter("cPShiChangSJ"));
		}
		String cPPingPai=request.getParameter("cPPingPai");
		String xingHao=request.getParameter("xingHao");
		String yanSe=request.getParameter("yanSe");
		String dingZhi=request.getParameter("dingZhi");
		String chuanYiRenQun=request.getParameter("chuanYiRenQun");
		int fengLeiId=0;
		if(request.getParameter("fengLeiId")!=null && !request.getParameter("fengLeiId").equals("")) {
			fengLeiId=Integer.parseInt(request.getParameter("fengLeiId"));
		}
		String cPImg=request.getParameter("cPImg");
		
		ShangPingXX spxx= new ShangPingXX(cPid, cPName, cPCaiZhi, cPShoujia, cPShiChangSJ, cPPingPai, xingHao, yanSe, dingZhi, chuanYiRenQun, fengLeiId, cPImg);
		int n =spxxDaoi.xiuGaiShangPingXX(spxx);
	    
	    if (n>0) {
			out.print(Result.toClient(true, "修改成功", ""));
		} else {
			out.print(Result.toClient(false, "修改失败", ""));

		}
	    out.flush();
	    out.close();
	}
	
}
